home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / smaltalk.lha / smalltalk-1.1.1 / mst.y < prev    next >
Text File  |  1991-09-14  |  10KB  |  418 lines

  1. /* -*- bison -*- */
  2.  
  3. /***********************************************************************
  4.  *
  5.  * Copyright (C) 1990, 1991 Free Software Foundation, Inc.
  6.  * Written by Steve Byrne.
  7.  *
  8.  * This file is part of GNU Smalltalk.
  9.  *
  10.  * GNU Smalltalk is free software; you can redistribute it and/or modify it
  11.  * under the terms of the GNU General Public License as published by the Free
  12.  * Software Foundation; either version 1, or (at your option) any later 
  13.  * version.
  14.  * 
  15.  * GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  16.  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
  17.  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  18.  * more details.
  19.  * 
  20.  * You should have received a copy of the GNU General Public License along with
  21.  * GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  22.  * Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  23.  *
  24.  ***********************************************************************/
  25.  
  26. %{
  27. #include "mst.h"
  28. #include "mstsym.h"
  29. #include "msttree.h"
  30. #include "mstdict.h"
  31. #include <stdio.h>
  32. #ifdef HAS_ALLOCA_H
  33. #include <alloca.h>
  34. #endif
  35.  
  36. #define YYDEBUG 1
  37.  
  38. extern Boolean        quietExecution;
  39.  
  40. %}
  41.  
  42. %pure_parser
  43.  
  44. %union{
  45.   char        cval;
  46.   double    fval;
  47.   long        ival;
  48.   char        *sval;
  49.   TreeNode    node;
  50. }
  51.  
  52. /* single definite characters */     
  53. %token BANG COLON UPARROW DOT ASSIGN SHARP SEMICOLON
  54. %token OPEN_PAREN CLOSE_PAREN OPEN_BRACKET CLOSE_BRACKET
  55. %token PRIMITIVE_START INTERNAL_TOKEN
  56.  
  57. /* larger lexical items */
  58. %token <sval> IDENTIFIER KEYWORD STRING_LITERAL SYMBOL_KEYWORD BINOP
  59.               VERTICAL_BAR 
  60. %token <ival> INTEGER_LITERAL
  61. %token <fval> FLOATING_LITERAL
  62. %token <cval> CHAR_LITERAL
  63.  
  64. %type <node> method message_pattern variable_name keyword_variable_list
  65.     temporaries variable_names statements non_empty_statements expression
  66.     assigns primary number symbol_constant symbol 
  67.     character_constant string array_constant array
  68.     array_constant_list block opt_block_variables 
  69.     block_variable_list unary_expression binary_expression
  70.     keyword_expression keyword_binary_object_description_list
  71.     cascaded_message_expression semi_message_list
  72.     message_elt simple_expression literal message_expression
  73.     array_constant_elt unary_object_description
  74.     binary_object_description
  75. %type <sval> unary_selector keyword binary_selector
  76. %type <ival> primitive
  77. %%
  78.  
  79. program:
  80.     class_definition_list
  81.     | internal_marker method    { compileMethod($2); }
  82.     ;
  83.  
  84. internal_marker:
  85.     INTERNAL_TOKEN            { clearMethodStartPos(); }
  86.  
  87. class_definition_list:
  88.     class_definition
  89.     | class_definition_list class_definition
  90.     ;
  91.  
  92. class_definition:
  93.     class_header method_list BANG
  94.     | class_header  BANG
  95.     | non_empty_statements BANG    { if (!hadError) {
  96.                         executeStatements(nil, $1,
  97.                                 quietExecution); 
  98.                       }
  99.                       hadError = false;
  100.                     }
  101.     | temporaries non_empty_statements BANG    
  102.                     { if (!hadError) {
  103.                         executeStatements($1, $2,
  104.                                 quietExecution); 
  105.                                           }
  106.                       hadError = false;
  107.                                         }
  108.     | error BANG            { hadError = false; }
  109.     ;
  110.  
  111. class_header:
  112.     BANG class_specification BANG     { clearMethodStartPos(); }
  113.     ;
  114.  
  115. class_specification:
  116.     simple_expression    { executeStatements(nil, 
  117.                     makeStatementList($1, nil), true); }
  118.     ;
  119.  
  120. /*
  121. method_list:
  122.     method                 { if (!hadError) {
  123.                         compileMethod($1);
  124.                       } else {
  125.                         hadError = false;
  126.                       }
  127.                     }
  128.         | method_list method        { if (!hadError) {
  129.                         compileMethod($2);
  130.                       } else {
  131.                         hadError = false;
  132.                       }
  133.                     }
  134.     ;
  135.      
  136. method:
  137.     message_pattern statements BANG 
  138.                     { $$ = makeMethod($1, nil, 0, $2); }
  139.     | message_pattern temporaries statements BANG
  140.                     { $$ = makeMethod($1, $2, 0, $3); }
  141.     | message_pattern primitive statements BANG
  142.                     { $$ = makeMethod($1, nil, $2, $3); }
  143.     | message_pattern temporaries primitive statements BANG
  144.         { $$ = makeMethod($1, $2, $3, $4); }
  145.     ;
  146.  
  147. */
  148. method_list:
  149.     method BANG             { if (!hadError) {
  150.                         compileMethod($1);
  151.                         clearMethodStartPos();
  152.                       } else {
  153.                         hadError = false;
  154.                       }
  155.                     }
  156.         | method_list method BANG    { if (!hadError) {
  157.                         compileMethod($2);
  158.                         clearMethodStartPos();
  159.                       } else {
  160.                         hadError = false;
  161.                       }
  162.                     }
  163.     ;
  164.      
  165. method:
  166.     message_pattern statements 
  167.                     { $$ = makeMethod($1, nil, 0, $2); }
  168.     | message_pattern temporaries statements
  169.                     { $$ = makeMethod($1, $2, 0, $3); }
  170.     | message_pattern primitive statements 
  171.                     { $$ = makeMethod($1, nil, $2, $3); }
  172.     | message_pattern temporaries primitive statements
  173.         { $$ = makeMethod($1, $2, $3, $4); }
  174.     ;
  175.  
  176.  
  177. message_pattern:
  178.     unary_selector            { $$ = makeUnaryExpr(nil, $1); }
  179.     | binary_selector variable_name    { $$ = makeBinaryExpr(nil, $1,
  180.                                       $2); }
  181.     | keyword_variable_list        { $$ = makeKeywordExpr(nil, $1); }
  182.     | error                { errorf("Invalid message pattern");
  183.                       hadError = true;
  184.                       $$ = nil; }
  185.     ;
  186.  
  187. unary_selector:
  188.     IDENTIFIER
  189.     ;
  190.  
  191. binary_selector:
  192.     BINOP            /* I don't like this usage of binop */
  193.     | VERTICAL_BAR
  194.     ;
  195.  
  196. variable_name:
  197.     IDENTIFIER            { $$ = makeVariable($1); }
  198.     ;
  199.  
  200. keyword_variable_list:
  201.     keyword variable_name        { $$ = makeKeywordList($1, $2); }
  202.     | keyword_variable_list keyword variable_name
  203.                     { addNode($1, makeKeywordList($2, $3));
  204.                       $$ = $1; }
  205.     ;
  206.  
  207. keyword:
  208.     KEYWORD
  209.     ;
  210.  
  211. primitive:
  212.     PRIMITIVE_START INTEGER_LITERAL BINOP
  213.                     { $$ = $2;
  214.                       if (strcmp($3, ">") != 0) {
  215.                         YYERROR;
  216.                       }
  217.                     }
  218.  
  219. temporaries:
  220.     VERTICAL_BAR VERTICAL_BAR    { $$ = nil; }
  221.     | VERTICAL_BAR variable_names VERTICAL_BAR
  222.                     { $$ = $2; }
  223.     ;
  224.  
  225. variable_names:
  226.     variable_name            { $$ = makeVariableList($1); }
  227.     | variable_names variable_name    { addNode($1, makeVariableList($2));
  228.                       $$ = $1; }
  229.     ;
  230.  
  231. statements:
  232.     /* empty */            { $$ = nil; }
  233.     | non_empty_statements
  234.     ;
  235.  
  236. non_empty_statements:
  237.     UPARROW expression     
  238.                 { $$ = makeStatementList(makeReturn($2),
  239.                                    nil); }
  240.     | expression        { $$ = makeStatementList($1, nil); }
  241.     | expression DOT statements
  242.                  /* I don't know if I like this production */
  243.                 { $$ = makeStatementList($1, $3); }
  244.     | error DOT statements  { $$ = $3;
  245.                   yyerrok;
  246.                   errorf("Error in expression");
  247.                   hadError = true;
  248.                 }
  249.     ;
  250.     
  251. expression:
  252.     simple_expression
  253.     | assigns simple_expression    { $$ = makeAssign($1, $2); }
  254.     ;
  255.  
  256. assigns:
  257.     variable_name ASSIGN        { $$ = makeVariableList($1); }
  258.     | assigns variable_name ASSIGN
  259.                     { addNode($1, makeVariableList($2));
  260.                       $$ = $1; }
  261.     ;
  262.  
  263. simple_expression:
  264.     primary
  265.     | message_expression
  266.     | cascaded_message_expression
  267.     ;
  268.  
  269. primary:
  270.     variable_name
  271.     | literal
  272.     | block                
  273.     | OPEN_PAREN expression CLOSE_PAREN { $$ = $2; }
  274.     ;
  275.  
  276. literal:
  277.     number
  278.     | symbol_constant
  279.     | character_constant
  280.     | string
  281.     | array_constant
  282.     ;
  283.  
  284. number:
  285.     INTEGER_LITERAL            { $$ = makeIntConstant($1); }
  286.     | FLOATING_LITERAL        { $$ = makeFloatConstant($1); }
  287.     ;
  288.  
  289. symbol_constant:
  290.     SHARP symbol            { $$ = makeSymbolConstant($2); }
  291.     ;
  292.  
  293. symbol:
  294.     IDENTIFIER            { $$ = internIdent($1); }
  295.     | binary_selector        { $$ = internBinOP($1); }
  296.     | SYMBOL_KEYWORD        { $$ = internIdent($1); }
  297.     | KEYWORD            { $$ = internIdent($1); }
  298.     ;
  299.  
  300.  
  301. character_constant:
  302.     CHAR_LITERAL            { $$ = makeCharConstant($1); }
  303.     ;
  304.  
  305. string:
  306.     STRING_LITERAL            { $$ = makeStringConstant($1); }
  307.     ;
  308.  
  309. array_constant:
  310.     SHARP array            { $$ = makeArrayConstant($2); }
  311.     ;
  312.  
  313. array:
  314.     OPEN_PAREN CLOSE_PAREN        { $$ = nil; }
  315.     | OPEN_PAREN array_constant_list CLOSE_PAREN
  316.                     { $$ = $2; }
  317.     ;
  318.  
  319. array_constant_list:
  320.     array_constant_elt        { $$ = makeArrayElt($1); }
  321.     | array_constant_list array_constant_elt
  322.                           { addNode($1, makeArrayElt($2));
  323.                       $$ = $1; }
  324.     ;
  325.  
  326. array_constant_elt:
  327.     number
  328.     | symbol
  329.     | string
  330.     | character_constant
  331.     | array
  332.     ;
  333.  
  334. block:
  335.     OPEN_BRACKET opt_block_variables statements CLOSE_BRACKET
  336.                     { $$ = makeBlock($2, $3); }
  337.     ;
  338.  
  339. opt_block_variables:
  340.     /* empty */            { $$ = nil; }
  341.     | block_variable_list VERTICAL_BAR
  342.     ;
  343.  
  344. /* syntax for blocks with temporaries is just args and vertical bar (if
  345.  * any followed by a standard temporaries declarations */
  346.  
  347. block_variable_list:
  348.     COLON variable_name        { $$ = makeVariableList($2); }
  349.     | block_variable_list COLON variable_name
  350.                         { addNode($1, makeVariableList($3));
  351.                       $$ = $1; }
  352.     ;
  353.  
  354. message_expression:
  355.     unary_expression
  356.     | binary_expression
  357.     | keyword_expression
  358.     ;
  359.  
  360. unary_expression:
  361.     unary_object_description unary_selector { $$ = makeUnaryExpr($1, $2); }
  362.     ;
  363.  
  364. unary_object_description:
  365.     primary
  366.     | unary_expression
  367.     ;
  368.  
  369. binary_expression:
  370.     binary_object_description binary_selector unary_object_description
  371.                     { $$ = makeBinaryExpr($1, $2, $3); }
  372.     ;
  373.  
  374. binary_object_description:
  375.     unary_object_description
  376.     | binary_expression
  377.     ;
  378.  
  379. keyword_expression:
  380.     binary_object_description keyword_binary_object_description_list
  381.                     { $$ = makeKeywordExpr($1, $2); }
  382.      ;
  383.  
  384. keyword_binary_object_description_list:
  385.     keyword binary_object_description
  386.                     { $$ = makeKeywordList($1, $2); }
  387.     | keyword_binary_object_description_list keyword
  388.       binary_object_description    { addNode($1, makeKeywordList($2, $3));
  389.                       $$ = $1; }
  390.     ;
  391.  
  392. cascaded_message_expression:
  393.     message_expression semi_message_list
  394.                        { $$ = makeCascadedMessage($1, $2); }
  395.     ;
  396.  
  397. semi_message_list:
  398.     SEMICOLON message_elt        { $$ = makeMessageList($2); }
  399.     | semi_message_list SEMICOLON message_elt
  400.                     { addNode($1, makeMessageList($3));
  401.                       $$ = $1; }
  402.     ;
  403.  
  404. message_elt:
  405.     unary_selector            { $$ = makeUnaryExpr(nil, $1); }
  406.     | binary_selector unary_object_description
  407.                     { $$ = makeBinaryExpr(nil, $1, $2); }
  408.     | keyword_binary_object_description_list
  409.                     { $$ = makeKeywordExpr(nil, $1); }
  410.     ;
  411.  
  412.  
  413. %%
  414. /*     
  415. ADDITIONAL C CODE
  416. */
  417.  
  418.